<?php
defined ( 'BASEPATH' ) or die ( 'forbidden' );
class newnoticemanager_Controller extends Controller {
    private $type;
    private $name;
    private $begintime;
    private $endtime;
    private $tmpcontent;
    private $content;
    private $tmpsequence;
    private $areaList;    //区号列表
    private $wholeServerHost;    //全局地址

    function _init() {
        $this->type = array (
                '1' => translate ( 'TYPE1' ),
                '2' => translate ( 'TYPE2' ) 
        );
        $sql = "select COUNT(id) AS num from tt_notice";
        $one = $this->db->one($sql);
        for ($i = 1; $i <= $one['num']; $i++)
        {
            $this->tmpsequence[$i] = $i;
        }
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
        $server_list=get_server_list();
        foreach($server_list as $k=>$row) {
            $this->areaList[$row['SvrOutAddr']] = $row['SvrID']."-".$row['SvrName'];
        }
    }
    function index() {
        $this->view->display ( "notice/newnoticemanager/index" );
    }
    function flexigrid() {
        list ( $colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page ) = $this->common->get_flexigrid_params ();

        $sql = "SELECT COUNT(id) AS num FROM tt_notice WHERE {$where}";
        $one = $this->db->one ( $sql );
        $total = $one ['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $sql = "SELECT {$colsinfo} FROM tt_notice WHERE {$where}";
        $this->db->query ( $sql );
        while ( $n = $this->db->get_array () ) {
            $n ['type'] = $this->type [$n ['type']];
            $row [] = array (
                    'id' => $n ['id'],
                    'cell' => $n 
            );
        }
        $result = array (
                'page' => $page,
                'total' => $total,
                'rows' => $row 
        );
        echo json_encode ( $result );
    }
    function add() {
        if (is_submit ()) {
            $rowdata = $_POST ['row'];
            //var_dump($rowdata);
            $game_host=$rowdata['area'];
            $rowdata['operator'] = $this->auth->fields['username'];
            $rowdata['inserttime'] = date('Y-m-d H:i:s', time());
            $rowdata['area']=json_encode($rowdata['area']);
            //$rowdata['tmpsequence'] = 1; //新添加的公告默认最前面显示
            
            $result = $this->db->insert ( 'tt_notice', $rowdata);
            $post_data['SessionID']="ewewqw";
            $post_data['SessionKey']="wqqew";
            $post_data['Type']=(int)$rowdata['type']; // 登陆公告：1 ,   运维公告 2
            $post_data['Interval']=(int)$rowdata['timesec'];
            $post_data['Count']=(int)$rowdata['tmpsequence'];
            $post_data['Notice']=(string)$rowdata['content'];
            $post_data['NoticeID']=(int)$rowdata['id'];

            //var_dump($post_data);

            if ($post_data['Type'] == 1){ // 登陆公告
                $post_url = $this->wholeServerHost.'gm_set_public_notice';
                echo "设置登陆公告的地址:$post_url";

                $pnResult = vpost($post_url,3,json_encode($post_data));
                if ($result and $pnResult ) {
                    cp_tips ("success");
                } else {
                    $result = translate ( 'C_TIPS_ERROR_TEXT' );
                    error ( $result );
                }
            }else{ // 运营公告
                foreach ($game_host as $key=>$value){
                    $postResult = vpost($value.'/gm_Send_Notice',3,json_encode($post_data));
                    if ($result and $postResult ) {
                        //insert_log ( translate ( 'MENU_NOTICE_NEWMANAGER' ), translate ( 'ADD' ), $rowdata );
                        //success ( translate ( 'C_TIPS_SUCCESS_TEXT' ) );
                        //$config = get_config();
                        // $URL_NOTICE = $config['host_url_backstage'] . "master/duoluotaitanGM/api/sendNoticeInfo";
                        #var_dump($URL_NOTICE);
                        #exit(0);
                        // $value = file_get_contents($URL_NOTICE);
                        cp_tips ("success");
                    } else {
                        $result = translate ( 'C_TIPS_ERROR_TEXT' );
                        error ( $result );
                    }
                }
            }
            //$postResult = vpost($_SESSION['area_selected_host'].'/gm_Send_Notice',3,json_encode($post_data));
        }

        $this->view->assign ( "type", $this->form->select ( "row[type]", $this->type, '', array ('id' => 'type') ) );
        $this->view->assign("arealist", $this->areaList);
        $this->view->display ( "notice/newnoticemanager/add" );
    }
    function edit($id = 0) {
        $arr = array();
        $sql = "select type,name,timesec,title,content,tmpsequence from tt_notice where id=$id";
        $row = $this->db->one($sql);
        unset($arr);
        foreach ($row as $key => $value) {
            $arr[$key] = $value;
        }
        if (is_submit()) {
            $rowdata = $_POST['row'];
            
            $rowdata['operator'] = $this->auth->fields['username'];
            $rowdata['inserttime'] = date('Y-m-d H-i-s', time());
            /////变序号的情况
            if ($rowdata['tmpsequence'] != $arr['tmpsequence'])
            {
                //把替换和被替换的notice的序列号换个位置
                $sql = "update tt_notice set tmpsequence = {$arr['tmpsequence']} where tmpsequence = {$rowdata['tmpsequence']}";
                $res = $this->db->execute($sql);
                if (!$res){
                    $result = translate('C_TIPS_ERROR_TEXT');
                    error($result);
                }
            }
            $result = $this->db->update('tt_notice', $rowdata, array('id' => $id));
            if ($result) {
                $one = $this->db->one("select type,name,timesec,title,content from tt_notice where id=$id");
                $one['title'] = $rowdata['title'];
                $one['content'] = $rowdata['content'];
                insert_log(translate('MENU_NOTICE_NEWMANAGER'),translate('EDIT'), $one);
                //success(translate('C_TIPS_SUCCESS_TEXT'));
                $config = get_config();
                $URL_NOTICE = $config['host_url_backstage'] . "master/duoluotaitanGM/api/sendNoticeInfo";
                #var_dump($URL_NOTICE);
                #exit(0);
                $value = file_get_contents($URL_NOTICE); 
                
                cp_tips ("success");
            } else {
                $result = translate('C_TIPS_ERROR_TEXT');
                error($result);
            }            
        }
        
        $this->view->assign('type', $this->form->select('row[type]', $this->type, array('type' => $arr['type']), array('id' => 'type', 'disabled' => 'disabled')));
        $this->view->assign('name', $arr['name']);
    /*    $this->view->assign('begintime', $arr['begintime']);
        $this->view->assign('endtime', $arr['endtime']);
        $this->view->assign('tmpcontent', $arr['tmpcontent']);*/
        $this->view->assign('timesec', $arr['timesec']);
        $this->view->assign('title', $arr['title']);
        $this->view->assign('tmpsequence', $this->form->select('row[tmpsequence]', $this->tmpsequence, array('tmpsequence' => $arr['tmpsequence']), array('id' => 'tmpsequence')));
        $this->view->assign('content', $arr['content']);
        $this->view->display("notice/newnoticemanager/edit");
    }
    function del($id = 0) {
        $c = 0;
        $r = '';
        $one = $this->db->one("select type,name,content,tmpsequence from tt_notice where id=$id");
        $mysequence = $one['tmpsequence'];
        //序号值大于此序号的所有公告序号都减一
        $sql = "UPDATE tt_notice SET tmpsequence = tmpsequence - 1 WHERE tmpsequence > {$mysequence}";
        $res = $this->db->execute($sql);
        if (!$res){
            $result = translate('C_TIPS_ERROR_TEXT');
            error($result);
        }
        
        $where = array('id' => $id);
        if ($this->db->delete('tt_notice', $where)) {
            insert_log(translate('MENU_NOTICE_NEWMANAGER'),translate('DEL'), array('id'=>$id));
            $c = 1;
            $r = translate('C_TIPS_SUCCESS_TEXT');
            $config = get_config();
            $URL_NOTICE = $config['host_url_backstage'] . "master/duoluotaitanGM/api/sendNoticeInfo";
            #var_dump($URL_NOTICE);
            #exit(0);
            $value = file_get_contents($URL_NOTICE);
            
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }
    
    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            if ($type == 'delete') {
                $this->db->query("DELETE FROM tt_notice WHERE id IN ({$ids})");
                insert_log(translate('MENU_NOTICE_NEWMANAGER'),translate('MULTI_DEL'),array('ids'=> $ids));
                $c = 1;
                $config = get_config();
                $URL_NOTICE = $config['host_url_backstage'] . "master/duoluotaitanGM/api/sendNoticeInfo";
                #var_dump($URL_NOTICE);
                #exit(0);
                $value = file_get_contents($URL_NOTICE);
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }
}







?>
